/*******************************************************************************
 *  DropdownElement class
 *******************************************************************************

// example options object
var options = {
    id: 'id',
    name: 'name',
    classes: 'classes',
    disabled: false,
    multiple: false,
    size: size,
    value: Array<OptionElement>,
    rules:{
        // validation rules here
        messages: {
            // validation messages here
        }
    },
    onchange: function(){}
}

*/

/**
 *  Represent dropdown model
 */
var DropdownElement = function(options) {
    
    // this class should extend an Element class
    $.extend(true, this, new Element($('<select>')))
    this.getElement()[0].wrapper = this;
    // ------------------------------------------------------- settings
    this.defaults = {
        name: options.id,
        multiple: false
    };
    
    var settings = this.prepareSettings(options);
    
    // ------------------------------------------------------- methods
    
    /**
     *  Sets list of options
     *  @param options defines list of options
     *  @return class instance
     */
    this.setList = function(options){
        if(!options) return this;
        var $select = this.getElement();
        
        this.clearList()
        for(var i=0; i < options.length; i++){
            $select.append(options[i].getElement());
        }
        return this;
    }
    
    this.clearList = function(){
        this.getElement().find('option').remove();
        return this;
    }
    
    // ------------------------------------------------------- element creation
    this.setId(settings.id)
        .addAttribute('name', settings.name)
        .addClass(settings.classes)
        .addAttribute('size', settings.size)
        .addAttribute('multiple', settings.multiple)
        .setList(settings.value);
        
    if(settings.disabled) this.disable();
    
    if(settings.onchange) {
        this.getElement()[0].onchange = settings.onchange;
    }
        
    // prepares element validation
    if(settings.rules){
        var metaobject = {
            validate: settings.rules
        };

        var metadata = JSON.stringify(metaobject);
        console.log(metadata);
        this.addClass(metadata)
    }
    
    console.log('DropdownElement was created with id = '+this.getId());
}